package com.neusoft.nep.controller;

import com.neusoft.nep.entity.ComboBoxUpdater;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class PublicSupervisorView1Controller {

    @FXML
    private ComboBox<String> provinceComboBox;
    @FXML
    private ComboBox<String> cityComboBox;
    @FXML
    private TextField addressTextField; // 添加具体地址输入框
    private ComboBoxUpdater comboBoxUpdater;
    private String phoneNumber; // 用于存储手机号
    private Map<String, Object> feedbackData = new HashMap<>();

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
//初始化combobox
    @FXML
    public void initialize() {
        comboBoxUpdater = new ComboBoxUpdater();
        provinceComboBox.setItems(comboBoxUpdater.getProvinceComboBox().getItems());
        cityComboBox.setItems(comboBoxUpdater.getCityComboBox().getItems());

        provinceComboBox.setOnAction(event -> {
            String selectedProvince = provinceComboBox.getValue();
            if (selectedProvince != null && comboBoxUpdater.getProvinceToCities().containsKey(selectedProvince)) {
                cityComboBox.setItems(comboBoxUpdater.getProvinceToCities().get(selectedProvince));
                if (!cityComboBox.getItems().isEmpty()) {
                    cityComboBox.setValue(cityComboBox.getItems().get(0));
                }
            }
        });

        if (!comboBoxUpdater.getProvinceComboBox().getItems().isEmpty()) {
            provinceComboBox.setValue(comboBoxUpdater.getProvinceComboBox().getItems().get(0));
            updateCities(provinceComboBox.getValue());
        }
    }
//按照省改变对应的市
    private void updateCities(String selectedProvince) {
        if (selectedProvince != null && comboBoxUpdater.getProvinceToCities().containsKey(selectedProvince)) {
            cityComboBox.setItems(comboBoxUpdater.getProvinceToCities().get(selectedProvince));
            if (!cityComboBox.getItems().isEmpty()) {
                cityComboBox.setValue(cityComboBox.getItems().get(0));
            }
        }
    }
//上传第一波数据
    @FXML
    public void handleNextButtonAction() {
        String selectedProvince = provinceComboBox.getValue();
        String selectedCity = cityComboBox.getValue();
        String specificAddress = addressTextField.getText(); // 获取具体地址

        if (selectedProvince != null && selectedCity != null && !specificAddress.isEmpty()) {
            feedbackData.put("省份", selectedProvince);
            feedbackData.put("城市", selectedCity);
            feedbackData.put("具体地址", specificAddress); // 添加具体地址到反馈数据中
            feedbackData.put("手机号", phoneNumber); // 将手机号添加到反馈数据中

            try {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("../view/PubilcSupervisorView2.fxml"));
                Parent root = loader.load();

                PubilcSupervisorView2Controller controller = loader.getController();
                controller.setFeedbackData(feedbackData);

                Stage stage = new Stage();
                stage.setScene(new Scene(root));
                stage.setTitle("PubilcSupervisorView2");
                stage.show();

            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("Please select all fields: Province, City, and enter specific address.");
        }
    }
}